﻿using System.Web;
using System.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Marbles.Core.Web.MVC.Enclosures;

namespace Marbles.Core.Web
{
    public class RequestInfo
    {

        public string QueryString
        {
            get { return this._QueryString; }
        }

        public string VirtualPath
        {
            get { return this._VirtualPath; }
        }

        public Enclosure Enclosure
        {
            get { return this._Enclosure; }
        }

        private Enclosure _Enclosure= null;
        private string _VirtualPath = string.Empty;
        private string _QueryString = string.Empty;

        public RequestInfo(HttpContext context, string requestType, string url, string pathTranslated)
        {
            string originalPath = HttpContext.Current.Request.Path.Trim();
            string pathInfo = context.Request.PathInfo.Trim();
            string queryString = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
            string EnclosureName = string.Empty;
            EnclosureFileParser EnclosureParser = new EnclosureFileParser();

            if (pathInfo != string.Empty)
            {
                context.Items["MarblesAction"] = pathInfo;
                this._VirtualPath = pathInfo + ".aspx";
            }
            else
            {
                context.Items["MarblesAction"] = originalPath;
                this._VirtualPath = originalPath + ".aspx";
            }

            EnclosureName = context.Items["MarblesAction"].ToString();

            if (EnclosureName.Substring(0, 1) == "/") { EnclosureName = EnclosureName.Substring(1, EnclosureName.Length - 1); }

            this._Enclosure = EnclosureParser[EnclosureName];            
            this._QueryString = queryString;

        }

        public bool HasEnclosure()
        {
            return (this._Enclosure != null);
        }
    }
}
